package com.ruoyi.platform.system.dept.manager;

import com.ruoyi.base.enums.ExceptionEnum;
import com.ruoyi.base.redis.dept.DeptRedis;
import com.ruoyi.base.service.BaseService;
import com.ruoyi.platform.system.dept.dao.DeptDAO;
import com.ruoyi.platform.system.dept.pojo.DeptDO;
import com.ruoyi.platform.system.dept.pojo.DeptDTO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 部门
 *
 * @author lcf
 */
@Component
public class DeptManager extends BaseService {
    @Autowired
    private DeptDAO deptDAO;
    @Autowired
    private DeptRedis deptRedis;

    /**
     * 检查部门id
     */
    public DeptDO checkDeptId(long deptId) {
        return getOrThrow(() -> deptDAO.selectDeptByPrimaryKey(deptId), ExceptionEnum.DEPT_NOT_EXIST);
    }

    /**
     * 根据id获取部门
     */
    public DeptDO getById(long deptId) {
        return deptDAO.selectDeptByPrimaryKey(deptId);
    }

    /**
     * 根据部门id列表获取部门列表
     */
    public List<DeptDO> getByDeptIdList(List<Long> deptIdList) {
        if (CollectionUtils.isEmpty(deptIdList)) {
            return Collections.emptyList();
        }

        return deptDAO.selectDeptByPrimaryKeyList(deptIdList);
    }

    /**
     * 获取指定部门部门树
     */
    public DeptDTO getDeptTree(long deptId) {
        List<DeptDO> deptAllList = deptDAO.selectByDept(null);
        if (deptId == 0) {
            DeptDO dept = new DeptDO();
            dept.setParentId(0L);
            List<DeptDO> deptList = deptDAO.selectByDept(dept);
            if (CollectionUtils.isEmpty(deptList)) {
                return null;
            }

            return getDeptTree(new DeptDTO(deptList.get(0)), deptAllList);
        } else {
            return getDeptTree(new DeptDTO(checkDeptId(deptId)), deptAllList);
        }
    }

    /**
     * 获取指定部门部门树
     *
     * @param allDeptList 全部部门
     */
    private DeptDTO getDeptTree(DeptDTO result, List<DeptDO> allDeptList) {
        List<DeptDTO> children = allDeptList.stream().filter(x -> Objects.equals(result.getDeptId(), x.getParentId()))
                .map(DeptDTO::new).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(children)) {
            return result;
        }

        result.setChildren(children);

        return result;
    }

    /**
     * 获取全部子部门
     */
    public List<DeptDTO> getChildList(long deptId) {
        List<DeptDO> deptList = deptRedis.getDeptList();
        List<DeptDTO> result = new ArrayList<>();

        getAllChildList(deptId, result, deptList);

        return result;
    }

    /**
     * 获取全部子部门
     */
    private void getAllChildList(long deptId, List<DeptDTO> result, List<DeptDO> deptList) {
        if (CollectionUtils.isEmpty(deptList)) {
            return;
        }

        List<DeptDTO> children = deptList.stream().filter(x -> Objects.equals(deptId, x.getParentId()))
                .map(DeptDTO::new).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(children)) {
            return;
        }

        result.addAll(children);
        children.forEach(x -> getAllChildList(x.getDeptId(), result, deptList));
    }
}
